package Leetcode.ArrayAndLinkedList.reverseList206;


import java.util.ArrayList;
import java.util.List;

/**
 * 该方法主要是将原链表转换为数组，将数组倒置，再生成一个链表返回
 */
public class Solution2 {
    public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null) {
            return head;
        }
        List list = new ArrayList();
        while (head != null) {
            list.add(head.val);
            head = head.next;
        }
        int[] nums = new int[list.size()];
        int j = 0;
        for(int i = list.size() - 1; i >= 0; i--) {
            nums[j++] = (int) list.get(i);
        }
        ListNode node = ListNode(nums);
        return node;
    }

    ListNode ListNode(int[] nums) {
        if (nums == null && nums.length <= 0) {
            return null;
        }

        ListNode head = new ListNode(nums[0]);
        for (int i = 1; i < nums.length; i++) {
            ListNode node = new ListNode(nums[i]);
            head.next = node;
            head = head.next;
        }
        return head;
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5};
        ListNode node = new ListNode(nums);
        System.out.println(node);
        Solution solution = new Solution();
        System.out.println(solution.reverseList(node));

    }
}
